import 'dart:io';

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:razer_one/app/main/constants/url_constants.dart';

class MainImageView extends StatelessWidget {
  String imageUrl;
  String? imageFilePath;
  double? width; //宽度
  double? height; //高度
  String? originImageName;
  String? placeholder;
  String? errorPlaceholder;
  final bool isShowBorder;
  final Color boderColor;
  MainImageView(this.imageUrl,
      {Key? key,
      this.imageFilePath,
      this.width,
      this.height,
      this.originImageName = "lib/app/main/images/commond/commond_banner.png",
      this.placeholder = "lib/app/main/images/commond/commond_banner.png",
      this.errorPlaceholder = "lib/app/main/images/commond/commond_banner.png",
      this.isShowBorder = false,
      this.boderColor = Colors.black})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width ?? 0,
      height: height ?? 0,
      decoration: isShowBorder
          ? BoxDecoration(
              borderRadius: BorderRadius.circular(5), //设置圆角
              border: Border.all(
                  width: 1, // 设置边框的宽度
                  color: boderColor // 设置边框的颜色
                  ))
          : null,
      child: buildImage(),
    );
  }

  buildImage() {
    //如果有拍照和相册照片
    if (imageFilePath != null) {
      return Image.file(File(imageFilePath!), fit: BoxFit.cover);
    }
    //如果网络url为空显示原始照片
    return imageUrl.isEmpty
        ? Image.asset(originImageName!, fit: BoxFit.cover)
        : CachedNetworkImage(
            imageUrl: kBaseURL + imageUrl,
            fit: BoxFit.cover,
            errorWidget: (context, url, error) {
              return Image.asset(errorPlaceholder!, fit: BoxFit.cover);
            },
            placeholder: (context, url) {
              return Image.asset(placeholder!, fit: BoxFit.cover);
            },
          );
  }
}
